﻿using System.Collections.Generic;
using Infrastructure.Attribute;
using Infrastructure.Extensions;
using Newtonsoft.Json.Linq;
using SqlSugar;
using ZR.Model.mes.md;
using ZR.Model.mes.md.DTO;
using ZR.Model.System;
using ZR.Service.mes.md.IService;
using ZR.Service.System.IService;
using NotImplementedException = System.NotImplementedException;

namespace ZR.Service.mes.md;

/// <summary>
/// 单位
/// </summary>
[AppService(ServiceType = typeof(IMdUnitService), ServiceLifetime = LifeTime.Transient)]
public class MdUnitService : BaseService<MdUnit>, IMdUnitService
{
    public int deleteunit(int[] ids)
    {
        return Delete(ids);
    }

    /// <summary>
    /// 获取列表
    /// </summary>
    /// <param name="pageNum"></param>
    /// <param name="pageSize"></param>
    /// <returns></returns>
    public MdUnitPageDTO GetList(string name,string code,int pageNum,int pageSize)
    {
        int totalNum = 0;
        var predicate = Expressionable.Create<MdUnit>()
            .AndIF(name.IsNotEmpty(),it=>it.MeasureName.Contains(name))
            .AndIF(code.IsNotEmpty(),it=>it.MeasureCode.Contains(code)).ToExpression();
      

        List<MdUnit> data = Context.Queryable<MdUnit>().Where(predicate).ToPageList(pageNum, pageSize, ref totalNum);
        MdUnitPageDTO unitPageDto = new MdUnitPageDTO();
        unitPageDto.list = data;
        unitPageDto.Total = totalNum;
        return unitPageDto ;
    }

    public List<MdUnit> GetList(SearchOptionDTO searchOption)
    {
        var predicate = Expressionable.Create<MdUnit>()
            .AndIF(searchOption.starttime.IsNotEmpty(), it => it.CreatedTime >= searchOption.starttime)
            .AndIF(searchOption.endtime.IsNotEmpty(), it => it.CreatedTime <= searchOption.endtime)
            .AndIF(searchOption.measureCode.IsNotEmpty(), it => it.MeasureCode.Contains(searchOption.measureCode))
            .AndIF(searchOption.measureName.IsNotEmpty(), it => it.MeasureCode.Contains(searchOption.measureName))
            .ToExpression();
        return  Context.Queryable<MdUnit>().Where(predicate).ToList();
    }

    /// <summary>
    /// 根据主键获取单条unit
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public MdUnit GetUnitbyPK(int id)
    {
        return GetId(id);
    }

    public int InsertUnit(MdUnit jo)
    {
        return Insert(jo);
    }

    public int Updateunit(MdUnit jo) => Update(jo,false);
}